package tech.tongyu.bob.demo.until.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.tongyu.bob.demo.common.Result;
import tech.tongyu.bob.demo.until.entity.ResObject;
import tech.tongyu.bob.demo.until.entity.Student;
import tech.tongyu.bob.demo.until.entity.User;

import java.util.Optional;
import java.util.SortedMap;
import java.util.TreeMap;

@RestController
@RequestMapping("/api")
@Slf4j
public class JsonToEntityController {
    public static final Gson gson = new Gson();

    /**
     * gson的例子
     * @return
     */
    @GetMapping("/gson")
    public Result gson(){
        User tongyu = User.builder().username("tongyu").password("123456").build();
        //转json
        String gsonJson = gson.toJson(tongyu);
        System.out.println(gsonJson);
        //json转实体
        System.out.println(gson.fromJson(gsonJson, User.class));
        Student stu = Student.builder().stuNo("123456").username("tongyu").password("123456").build();
        //json转实体
        User user = gson.fromJson(gson.toJson(stu), User.class);

        return Result.success(ResObject.builder().res1(gsonJson).res2(gson.fromJson(gsonJson, User.class)).res3(user).build());
    }

    @GetMapping("/hutool")
    public Result hutool(){
        SortedMap<Object, Object> sortedMap = new TreeMap<Object, Object>() {
            private static final long serialVersionUID = 1L;
            {
                put("attributes", "a");
                put("b", "b");
                put("c", "c");
            }};
        log.info("{}", JSONUtil.toJsonPrettyStr(sortedMap));
        User tongyu = User.builder().username("tongyu").password("123456").build();
        log.info("{}", JSONUtil.toJsonStr(tongyu));
        String jsonStr = JSONUtil.toJsonStr(tongyu);
        User bean = JSONUtil.toBean(jsonStr, User.class);
        log.info("{}", bean);
        return Result.success();
    }

    @GetMapping("/fastjson2")
    public Result fastjson2(){
        SortedMap<Object, Object> sortedMap = new TreeMap<Object, Object>() {
            private static final long serialVersionUID = 1L;
            {
                put("attributes", "a");
                put("b", "b");
                put("c", "c");
            }};
        log.info("{}", JSON.toJSON(sortedMap));
        User tongyu = User.builder().username("tongyu").password("123456").build();
        log.info("{}", JSON.toJSONBytes(tongyu));
        String jsonStr = JSON.toJSONString(tongyu);
        User bean = JSON.parseObject(jsonStr, User.class);
        log.info("{}", bean);

        String result = Optional.ofNullable("Hello")
                .map(String::toUpperCase)     // 如果值存在，将其转换为大写
                .filter(value -> value.startsWith("H"))  // 过滤以 "H" 开头的值
                .orElse("Default Value");  // 如果最后的结果为空，则返回默认值

        System.out.println(result);  // 输出 "HELLO"

        return Result.success();
    }
}
